<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        /* .layui-table {
            background-color: #f0dfd1;
        } */
    </style>
</head>
<body>
<!--<div class="layuimini-container" style="background-color: #f0dfd1">-->
</body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="queryFormId">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <!--
                            <div class="layui-input-inline">
                                <input type="text" name="search" autocomplete="off" placeholder="用户名/手机号" class="layui-input">
                            </div>
                            -->
                        </div>
                        <div class="layui-inline" id="toolbarDemo">
                            <!--<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>-->
                            <button type="button" class="layui-btn data-add-btn" onclick="addData()" > 添加用户 </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../lib/js/config.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'table', 'miniAdmin'], function () {
        var $ = layui.jquery,
            form = layui.form,
            miniAdmin = layui.miniAdmin,
            table = layui.table;

        var city = getCity($);
        var district = getDistrict($);
        var tableIns = table.render({
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,         //返回状态码
                    "msg": res.data,          //解析提示信息
                    "count": res.count,       //后台返回的数据总条数，用于自定义分页使用
                    "data": res.data          //解析数据列表
                };
            },
            elem: '#currentTableId',
            url: usersDataURL+"?city="+city+"&district="+district,
            cols: [[
                {field: 'id', title: 'ID', hide: true, edit: false},
                {field: 'name', width: 120, title: '用户名', edit: false},
                {field: 'sex', width: 80, title: '性别'},
                {field: 'age', width: 60, title: '年龄'},
                {field: 'height', width: 60, title: '身高'},
                {field: 'weight', width: 60, title: '体重'},
                {field: 'address', width: 80, title: '地址'},
                {field: 'id_number', hide: true, title: '身份证号'},
                {field: 'edu_background', width: 100, title: '文化程度'},
                {field: 'is_instructor', width: 100, title: '是否指导员'},
                {field: 'instructor_class', width: 100, title: '指导员等级'},
                {field: 'instructor_type', width: 100, title: '指导员类型'},
                {field: 'ver_status', width: 100, title: '审核状态'},
                {field: 'province', width: 100, title: '省级区划'},
                {field: 'city', width: 100, title: '市级区划'},
                {field: 'village', width: 100, title: '乡镇'},
                {field: 'certificate_num', width: 135, title: '证书编号'},
                {field: 'certificate_date', width: 135, title: '发证日期'},
                {field: 'gym', width: 135, title: '健身地点'},
                {field: 'assessment', width: 135, title: '评定成绩'},
                {field: 'upload_time', width: 135, title: '上传日期'},
                {field: 'annual_survey_date', width: 135, title: '年检日期'},
                {title: '操作', minWidth: 50, toolbar: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            curr: 1,
            page: true,
            done: function () {
                $("[data-field = 'sex']").children().each(function() {
                    if ($(this).text() == "male") {
                        $(this).text("男");
                    } else if ($(this).text() == 'female') {
                        $(this).text("女");
                    }
                })
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var queryData = $('#queryFormId').serializeArray();
            var assemblyData = {};
            $.each(queryData, function(index, item) {
                assemblyData[item.name]=item.value
            });
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: assemblyData
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/table/add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var newData = {};
                Object.keys(data).map(function (key) {
                    if(data[key]!=null) {
                        newData[key]= data[key];
                    }
                });
                localStorage.setItem("editUserData", JSON.stringify(newData));
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/table/edit.html',
                });
                // $(window).on("resize", function () {
                //     layer.full(index);
                // });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除吗', function (index) {
                    $.getJSON(userDeleteURL+"?id="+data.id, function (data) {
                        if (responseSuccess(miniAdmin, data) === "success") {
                            layer.close(index);
                            var queryData = $('#queryFormId').serializeArray();
                            var assemblyData = {};
                            $.each(queryData, function(index, item) {
                                assemblyData[item.name]=item.value
                            });
                            //执行搜索重载
                            table.reload('currentTableId', {
                                page: {
                                    curr: 1
                                },
                                where: assemblyData
                            }, 'data');
                            return false;
                        }
                    });
                });
            }
        });
    });

    function addData() {
        var index = layer.open({
            title: '添加用户',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: '/page/table/add.html',
        });
        // $(window).on("resize", function () {
        //     layer.full(index);
        // });
    }
    
    function getCity($) {
        /**
        console.log(parent.document.getElementById("layuiminiIframe").contentWindow.location);
        console.log(parent.document.getElementById("layuiminiIframe").contentWindow.location.search);
        var allParamsStr = parent.document.getElementById("layuiminiIframe").contentWindow.location.search.substring(1);
        console.log(decodeURIComponent(allParamsStr));
        var city = getQueryVariable(allParamsStr, "city");
        */
        var ele = $(".layuimini-tab .layui-tab-title li.layui-this", parent.document);
        if (ele.length > 0) {
            var layId = $(ele[0]).attr('lay-id');
            var arr = layId.split('?');
            if(arr.length>1) {
                return getQueryVariable(arr[1], "city");
            }
        }
        return "";
    }

    function getDistrict($) {
        /**
        var allParamsStr = parent.document.getElementById("layuiminiIframe").contentWindow.location.search.substring(1);
        console.log(decodeURIComponent(allParamsStr));
        var district = getQueryVariable(allParamsStr, "district");
        */
        var ele = $(".layuimini-tab .layui-tab-title li.layui-this", parent.document);
        if (ele.length > 0) {
            var layId = $(ele[0]).attr('lay-id');
            var arr = layId.split('?');
            if(arr.length>1) {
                return getQueryVariable(arr[1], "district");
            }
        }
        return "";
    }

    function getQueryVariable(allParamsStr, variableName) {
       var vars = allParamsStr.split("&");
       for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variableName) {
                return pair[1];
            }
       }
       return "";
    }
</script>
<script>

</script>

</body>
</html>